
export function getBinaryAsString(num: number, bitLen: number = 8): string {
  return num.toString(2).padStart(bitLen, "0");
}

export function getBinaryStringFrom(
  numA: number,
  numB: number,
  order = "<H"
): string {
  let strA = getBinaryAsString(numA);
  let strB = getBinaryAsString(numB);
  if (order === "<H") {
    return strB + strA;
  }
  return strA + strB;
}

/**
 * @description 注意，该方法会调整 参数 numArr
 * @param numArr 
 * @returns 
 */
export function readAsCharCode(numArr: Array<number>) {
  let firstOne = numArr.shift()!;
  let secondOne = numArr.shift()!;
  return getCharCodeFrom(
    firstOne,
    secondOne,
    "<H"
  );
}

export function getCharCodeFrom(
  numA: number,
  numB: number,
  order = "<H"
): number {
  let binaryString = getBinaryStringFrom(numA, numB, order);
  return parseInt(binaryString, 2);
}

export function getStringCharFrom(
  numA: number,
  numB: number,
  order = "<H"
): string {
  const charCode = getCharCodeFrom(numA, numB, order);
  return String.fromCharCode(charCode);
}

export function getCharNumFrom(
  numA: number,
  numB: number,
  order = "<H"
): number {
  const str = getStringCharFrom(numA, numB, order);
  return parseInt(str);
}

export function getCharCodeFromCoupleBuf(bufU16arr: Uint16Array): Array<number> {
  let len = bufU16arr.length;
  let arr: Array<number> = [];
  for (let i = 0; i < len; i += 2) {
    let charCode = getCharCodeFrom(
      bufU16arr[i],
      bufU16arr[i + 1]
    );
    arr.push(charCode);
  }
  return arr;
}
